﻿using SdmxMl.Common;
using System.Windows.Forms;
using SdmxMl.Structure;
using SdmxMl.Manager;

namespace SdmxMl.Helper
{
    public class TreeHelper
    {
        public static void toolStripButtonExpandCollapse(TreeView tv)
        {
            if (tv.SelectedNode != null)
            {
                tv.BeginUpdate();

                // if no child operate on parent if possible
                TreeNode n = tv.SelectedNode;
                if (n.Nodes.Count == 0 && n.Parent != null)
                {
                    n = n.Parent;
                    tv.SelectedNode = n;
                }

                if (n.IsExpanded)
                    n.Collapse();
                else
                    n.ExpandAll();
                n.EnsureVisible();
                tv.EndUpdate();
            }

        }

        public static Artefact GetParentArtefact(TreeNode node)
        {
            Artefact parent = null;
            while (node != null && node.Parent != null)
            {
                if (node.Parent.Tag is Artefact)
                {
                    parent = node.Parent.Tag as Artefact;
                    break;
                }
                node = node.Parent;
            }

            return parent;
        }

        public static ArtefactManager GetParentArtefactManager(TreeNode node)
        {
            ArtefactManager parent = null;
            while (node != null && node.Parent != null)
            {
                if (node.Parent.Tag is ArtefactManager)
                {
                    parent = node.Parent.Tag as ArtefactManager;
                    break;
                }
                node = node.Parent;
            }

            return parent;
        }
        public static TreeNode GetFlatHierarchicalListNode(TreeNode node)
        {
            TreeNode target = null;
            while (node != null && node.Parent != null)
            {
                if (node.Tag is FlatHierarchicalList)
                {
                    target = node;
                    break;
                }
                node = node.Parent;
            }

            return target;
        }


        public static void CountNodes(TreeNode node, ref int count)
        {
            if (node.Nodes.Count > 0)
            {
                count += node.Nodes.Count;
                foreach (TreeNode nd in node.Nodes)
                    CountNodes(nd, ref count);
            }
        }

        public static TreeNode GetHierarchyManage(TreeNode node)
        {
            TreeNode target = null;
            while (node != null && node.Parent != null)
            {
                if (node.Tag is IHierarchyManage)
                {
                    target = node;
                    break;
                }
                node = node.Parent;
            }

            return target;
        }

        public static TreeNode GetParentArtefactNode(TreeNode node)
        {
            TreeNode parent = null;
            while (node != null && node.Parent != null)
            {
                if (node.Parent.Tag is Artefact)
                {
                    parent = node.Parent;
                    break;
                }
                node = node.Parent;
            }

            return parent;
        }

        public static void LocateObject(TreeNode node, object o, ref TreeNode targetNode)
        {
            if (node.Tag == o)
                targetNode = node;
            else
            {
                foreach (TreeNode n in node.Nodes)
                {
                    if (targetNode == null)
                        LocateObject(n, o, ref targetNode);
                }
            }

        }

        public static void LocateObject(TreeView tv, object o, ref TreeNode targetNode)
        {
            foreach (TreeNode n in tv.Nodes)
            {
                if (targetNode == null)
                    LocateObject(n, o, ref targetNode);
            }
        }

        public static void LocateText(TreeNode node, string txt, ref TreeNode targetNode)
        {
            if (node.Text == txt)
                targetNode = node;
            else
            {
                foreach (TreeNode n in node.Nodes)
                {
                    if (targetNode == null)
                        LocateText(n, txt, ref targetNode);
                }
            }

        }

        public static string CalculateNodeChilds(TreeNode node)
        {
            int total = 0;
            int level = 0;

            if (node != null)
            {
                level = node.Nodes.Count;
                TreeHelper.CountNodes(node, ref total);
            }

            string msg = string.Empty;
            if (level > 0)
            {
                if (level == total)
                    msg = string.Format("Child Count: {0}", total);
                else
                    msg = string.Format("Child Count: {0} - Total: {1}", level, total);
            }
            return msg;
        }

        
    }
}
